package com.rain.activity.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.rain.activity.MainActivity;
import com.rain.activity.QuestionActivity;
import com.rain.activity.R;
import com.rain.activity.SearchActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Rain on 2016/4/18.
 */
public class BaseActivity extends AppCompatActivity{
    private int i;
    private Class[] targetActivity = new Class[]{
        MainActivity.class, QuestionActivity.class, SearchActivity.class};
    private int [] btnImgsSelect = new int[]{R.drawable.bt01_b,R.drawable.bt02_b,R.drawable.bt03_b};
    private int [] btnImgs = new int[]{R.drawable.bt01_a,R.drawable.bt02_a,R.drawable.bt03_a};
    private Button[] btns = new Button[3];
    private TextView topTitle;
    private Button setting ;
    private PopupWindow window;
    private List<Activity> activities = new ArrayList<>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        activities.add(this);
        super.onCreate(savedInstanceState);

    }
    protected  void init(String title,int index){
        if(topTitle==null){  topTitle = (TextView) findViewById(R.id.toptitle);}
      else
            topTitle.setText(title);
        btns[0]= (Button) findViewById(R.id.bt1);
        btns[1]= (Button) findViewById(R.id.bt2);
        btns[2]= (Button) findViewById(R.id.bt3);
        for(i =0;i<btns.length;i++){
            final int finalI = i;
            if(i==index){
                btns[i].setBackgroundResource(btnImgsSelect[i]);
            }
            else{
                btns[i].setBackgroundResource(btnImgs[i]);

                btns[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent in = new Intent(BaseActivity.this,targetActivity[finalI]);
                        startActivity(in);
                    }
                });
            }
        }
        setting = (Button) findViewById(R.id.setting);
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window = new PopupWindow(Globals.SCREEN_WIDTH/4,Globals.SCREEN_HEIGHT/6);
                View view = LayoutInflater.from(BaseActivity.this).inflate(R.layout.win_list,null);
                window.setContentView(view);
                if(window.isShowing()){
                    window.dismiss();
                }
                else{
                    window.showAsDropDown(setting);
                }
                TextView win_version = (TextView) view.findViewById(R.id.win_version);
                TextView win_about = (TextView) view.findViewById(R.id.win_about);
                TextView win_exit = (TextView) view.findViewById(R.id.win_exit);
                win_version.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();
                        Toast.makeText(BaseActivity.this,"1.1版本",Toast.LENGTH_LONG).show();
                    }
                });
                win_about.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();
                        AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
                        builder.setTitle("关于我们");
                        builder.setMessage("北航大数据");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        builder.create().show();
                    }
                });
                win_exit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();
                        for (Activity a:activities){
                            finish();
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void onDestroy() {
        activities.remove(this);
        super.onDestroy();
    }
}
